package opc.read.utils;

import java.io.*;

/**
 * @Description: 将字符串写到文本文件的工具类
 * @author: liyuan
 * @data 2020-12-30 16:29
 */
public class WriterFile {
    public static boolean writeTxtFile(String content, File fileName, String encoding) {

        boolean result = false;
        if (!fileName.getParentFile().exists()) {
            fileName.getParentFile().mkdirs();
        }

        FileOutputStream o = null;
//        BufferedWriter o = null;
//        FileWriter o = null;
        try {

            o = new FileOutputStream(fileName, true);
//            o = new BufferedWriter(new FileWriter(fileName, true));
//            o = new FileWriter(fileName, true);
            o.write(content.getBytes(encoding));
//            o.write(content);
            o.write('\n');
            o.flush();
            result = true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (o != null) {
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return result;
    }
}
